<?php
/**
 * @title 508. 出现次数最多的子树元素和 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param TreeNode $root
     * @return Integer[]
     */
    function findFrequentTreeSum($root) {
        if($root === null){
            return [];
        } else {}

        /**
         * @since 2022-06-21 每个子树的元素和
         */
        $this->sumArray = [];

        /**
         * @since 2022-06-21 统计子树元素和
         */
        self::find($root);

        /**
         * @since 2022-06-21 统计相同和出现的次数
         */
        $cntArray = [];
        foreach ($this->sumArray as $sum){
            if(isset($cntArray[$sum])){
                $cntArray[$sum] += 1;
            } else {
                $cntArray[$sum] = 1;
            }
        }

        /**
         * @since 2022-06-21 出现次数最多
         */
        $maxCnt = 0;
        $result = [];
        foreach ($cntArray as $sum=>$cnt){
            if($cnt > $maxCnt){
                $maxCnt = $cnt;
                $result = [$sum];
            } elseif($cnt == $maxCnt) {
                $result[] = $sum;
            } else {}
        }

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 统计子树元素之和
     * @author start2004
     * @since 2022-06-21 Created
     *
     * @param $tree
     * @return mixed
     */
    function find($tree){
        $val = $tree->val;

        /**
         * @since 2022-06-21 左子树
         */
        if($tree->left !== null){
            $val += self::find($tree->left);
        } else {}

        /**
         * @since 2022-06-21 右子树
         */
        if($tree->right !== null){
            $val += self::find($tree->right);
        } else {}

        /**
         * @return
         */
        $this->sumArray[] = $val;
        return $val;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202206/2022.06.19.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
